package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domaine.Joueur;
import domaine.Partie;
import domaine.Partie.Etat;

import usecases.GestionJoueur;
import usecases.GestionPartie;

/**
 * Servlet implementation class Connexion
 */
@WebServlet("/journal.html")
public class JournalPartie extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;
	@EJB
	private GestionPartie gestionPartie;
	@EJB
	private GestionJoueur gestionJoueur;

	/**
	 * Default constructor.
	 */
	public JournalPartie() {
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nbPartie = request.getParameter("NbPartie");
		int intNombrePartie = 0;
		if (nbPartie == "" || nbPartie == null) {
			response.sendRedirect(response.encodeRedirectURL("parties.html"));
			return;
		}
		intNombrePartie = Integer.parseInt(nbPartie);
		if (intNombrePartie == 0) {
			response.sendRedirect(response.encodeRedirectURL("parties.html"));
			return;
		} else {
			String idStringPartie = request.getParameter("journalSelect");
			if (idStringPartie == "" || idStringPartie == null) {
				request.setAttribute("message",
						"Vous devez selectionner une partie");
				RequestDispatcher rd = getServletContext().getNamedDispatcher(
						"parties.html");
				rd.forward(request, response);
				return;
			}
			int idPartie = Integer.parseInt(idStringPartie);
			String journal = gestionPartie.visualiserJournalPartie(idPartie);
			request.setAttribute("idPartie", idPartie);
			request.setAttribute("journal", journal);
			RequestDispatcher rd = getServletContext().getNamedDispatcher(
					"journalPartie.html");
			rd.forward(request, response);
		}
	}

}
